// pages/upload/upload.js
import weCropper from '../../config/weCropper.js';
const device = wx.getSystemInfoSync();
const width = device.windowWidth;
const height = device.windowHeight - 50;
let app = getApp()
let yuming = app.globalData.yuming
console.log('获取')
console.log(device);
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cropperOpt: {
      id: 'cropper',
      width: width,
      height: height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 300) / 2,
        y: (height - 330) / 2,
        width: 293,
        height: 186
      }
    },
    weCropper: ''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    console.log(option.src)
    this.setData({
      d: option.d
    })
    // do something
    const cropperOpt = this.data.cropperOpt;
    const src = option.src;
    if (src) {
      Object.assign(cropperOpt, {
        src
      });

      this.weCropper = new weCropper(cropperOpt)
        .on('ready', function (ctx) {})
        .on('beforeImageLoad', ctx => {
          wx.showToast({
            title: '上传中',
            icon: 'loading',
            duration: 3000
          });
        })
        .on('imageLoad', ctx => {
          wx.hideToast();
        });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  touchStart(e) {
    this.weCropper.touchStart(e);
  },
  touchMove(e) {
    this.weCropper.touchMove(e);
  },
  touchEnd(e) {
    this.weCropper.touchEnd(e);
  },
  convertBase64UrlToBlob(dataURI, type) {
    var binary = atob(dataURI.split(',')[1]);
    var array = [];
    for (var i = 0; i < binary.length; i++) {
      array.push(binary.charCodeAt(i));
    }
    return new Blob([new Uint8Array(array)], {
      type: type
    }, {
      filename: '1111.jpg'
    });
  },
  blobToDataURL(blob) {
    var a = new FileReader();
    a.readAsDataURL(blob); //读取文件保存在result中
    a.onload = function (e) {
      var getRes = e.target.result; //读取的结果在result中
      console.log(getRes);
    };
  },
  getCropperImage() {
    console.log(777777777777777777777)
    let that = this;
    //let pathurl = url + '/user/upload';上传服务器地址
    this.weCropper.getCropperImage(avatar => {
      if (avatar) {
        var uper = wx.uploadFile({
          url: yuming + 'Besall/Upload',
          filePath: avatar,
          name: "file",
          header: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Token': that.data.token,
            source: 'EabEnd'
          },
          formData: {
            file: avatar
          },
          success(res) {
            var data = JSON.parse(res.data)
            console.log(data)
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
            that.setData({
              frontphoto: data.httpUrl + data.imgUrl,
              fro: data.imgUrl,
            })
            console.log(that.data.frontphoto)
            wx.request({
              url: yuming + 'shop/shopSaveBossBg',
              method: 'POST',
              header: {
                'Content-Type': 'application/x-www-form-urlencoded',
                token: wx.getStorageSync('token')
              },
              data: {
                shopId: wx.getStorageSync('shopId'),
                bossBackg: that.data.fro
              },
              success: function (res) {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
                setTimeout(function () {
                  wx.reLaunch({
                    url: '/page_two/bossquan/bossquan',
                  })
                }, 1000)
              },
              fail: function () {
                app.consoleLog("请求数据失败");
              }
            })
            // that.wenjian()
          },
          complete(res) {
            // console.log(res)
          },
        })
      } else {
        console.log('获取图片失败，请稍后重试');
        wx.showToast({
          title: '获取图片失败，请稍后重试',
          icon: 'none'
        })
      }
    });
  },
  // xiugaiphoto: function (is_info) {
  //   var tt = this;
  //   this.configure.ajax({
  //     url: 'api/comm/up_touxiang',
  //     tishi: 1,
  //     data: {
  //       type: 2,
  //       img: is_info
  //     },
  //     method: 'POST',
  //     success: function (res) {
  //       uni.navigateBack();
  //     }
  //   })
  // },
  uploadTap() {
    const self = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        let src = res.tempFilePaths[0];
        //  获取裁剪图片资源后，给data添加src属性及其值
        self.weCropper.pushOrign(src);
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})